﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using FrienDev.BusinessRules;
using FrienDev.Data;
using System.Collections.Generic;

public partial class Controls_Club_List : System.Web.UI.UserControl
{
    //public bool m_isFriendList = false;
    //private int m_PageSize = 10;

    //public bool IsFriendList
    //{
    //    set
    //    {
    //        m_isFriendList = value;
    //    }
    //}

    protected void Page_Load(object sender, EventArgs e)
    {
        BindPager();
    }

    private void BindPager()
    {
        if (HttpContext.Current.Request["type"] != null)
        {
            int type = Convert.ToInt32(HttpContext.Current.Request["type"]);
            string userid = GetUserId();
            Pager.RecordCount = ClubRules.Instance.GetClubListCountByUserIdAndType(userid, type);
        }
        else
        {
            string userid = GetUserId();
            Pager.RecordCount = ClubRules.Instance.GetClubListCountByUserId(userid);
        }
        result.Text = "共有" + Pager.RecordCount + "个俱乐部。";
    }

    private void BindData()
    {
        if (HttpContext.Current.Request["type"] != null)
        {
            int type = Convert.ToInt32(HttpContext.Current.Request["type"]);
            //if (!m_isFriendList)
            //{
                string userid = GetUserId();
                //Pager.RecordCount = ClubRules.Instance.GetClubListCountByUserIdAndType(userid, type);
                DataList1.DataSource = ClubRules.Instance.GetClubListByUserIdAndType(userid, type, Pager.CurrentPageIndex, Pager.PageSize);
            //}
            //else
            //{
            //    Pager.RecordCount = ClubRules.Instance.GetFriendClubListCountByUserIdAndType(UserRules.Instance.GetCurrentUserId(), type);
            //    DataList1.DataSource = ClubRules.Instance.GetFriendClubListByUserIdAndType(UserRules.Instance.GetCurrentUserId(), type,Pager.CurrentPageIndex, Pager.PageSize);
            //}
        }
        else
        {
            //if (!m_isFriendList)
            //{
                string userid = GetUserId();
                //Pager.RecordCount = ClubRules.Instance.GetClubListCountByUserId(userid);
                DataList1.DataSource = ClubRules.Instance.GetClubListByUserId(userid, Pager.CurrentPageIndex, Pager.PageSize);
            //}
            //else
            //{
            //    Pager.RecordCount = ClubRules.Instance.GetFriendClubListCountByUserId(UserRules.Instance.GetCurrentUserId());
            //    DataList1.DataSource = ClubRules.Instance.GetFriendClubListByUserId(UserRules.Instance.GetCurrentUserId(), Pager.CurrentPageIndex, Pager.PageSize);
            //}
        }
        DataList1.DataBind();
    }

    private string GetUserId()
    {
        string userid = "";
        if (HttpContext.Current.Request["uid"] != null)
        {
            userid = HttpContext.Current.Request["uid"];
        }
        else
        {
            userid = UserRules.Instance.GetCurrentUserId();
        }
        return userid;
    }

    protected void Pager_PageChanged(object sender, EventArgs e)
    {
        BindData();
    }


    //protected void HandleDataBind(object sender, EventArgs e)
    //{
    //    HyperLink hy = sender as HyperLink;
    //    hy.NavigateUrl = "/Club/Detail.aspx?clubId=" + hy.NavigateUrl;
    //}

    //protected void HandleImgDataBind(object sender, EventArgs e)
    //{
    //    Image img = sender as Image;
    //    img.ImageUrl = "/upload/club/" + img.ImageUrl;
    //}

    //protected void HandleCityBind(object sender, EventArgs e)
    //{
    //    Label la = sender as Label;
    //    la.Text = UserRules.Instance.GetCityByCityId(Convert.ToInt32(la.Text)).CityName;
    //}

    //protected void HandleAmountBind(object sender, EventArgs e)
    //{
    //    Label la = sender as Label;
    //    la.Text = ClubRules.Instance.GetClubMemberAmountByClubId(Convert.ToInt32(la.Text)).ToString()+"人";
    //}

    //protected void HandleClubHeadBind(object sender, EventArgs e)
    //{
    //    Label la = sender as Label;
    //    la.Text = ClubRules.Instance.GetClubHeadByClubId(Convert.ToInt32(la.Text));
    //}

    protected void HandleEditBind(object sender, EventArgs e)
    {
        HyperLink hy = sender as HyperLink;
        int m_ClubId = Convert.ToInt32(hy.NavigateUrl);
        //Club club = ClubRules.Instance.GetClubByClubId(m_ClubId);
        IEnumerable<ClubMember> m_ClubInfo = ClubRules.Instance.GetCurrentUserInfo(m_ClubId);
        if (m_ClubInfo.Count() > 0)
        {
            //join.Visible = false;
            //arrow.Visible = false;
            if ((m_ClubInfo.First()).ClubMemberTypeId == ClubRules.m_1LevelMemberTypeId)
            {
                hy.Visible = true;
                hy.NavigateUrl = "/Club/InfoEdit.aspx?clubId=" + m_ClubId;
            }
            else
            {
                hy.Visible = false;
            }
        }
    }
    //protected void DataList1_ItemDataBound(object sender, DataListItemEventArgs e)
    //{
        
    //}

    void lb_Click(object sender, EventArgs e)
    {
        LinkButton lb = sender as LinkButton;
        ClubRules.Instance.QuitClubByUserIdAndClubId(UserRules.Instance.GetCurrentUserId(), Convert.ToInt32(lb.CommandArgument));
        Response.Redirect("/Club/Default.aspx");
    }
    //protected void DataList1_PageIndexChanging(object sender, GridViewPageEventArgs e)
    //{
    //    DataList1.PageIndex = e.NewPageIndex;
    //    DataList1.DataBind();//这里会执行pageload中的数据绑定（因为pageload中未判断postback）
    //}
    protected void DataList1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            ClubMember club = e.Row.DataItem as ClubMember;
            if (ClubRules.Instance.GetIsNormalMemberByUserIdAndClubId(club.ClubId, UserRules.Instance.GetCurrentUserId()))
            {
                LinkButton lb = e.Row.FindControl("quitLink") as LinkButton;
                lb.Visible = true;
                lb.CommandArgument = club.ClubId.ToString();
                lb.Click += new EventHandler(lb_Click);
            }
            (e.Row.FindControl("LabelCity") as Label).Text = club.Club.City.CityName;
            (e.Row.FindControl("ImageLogo") as Image).ImageUrl = "/upload/club/" + club.Club.LogoFileName;
            (e.Row.FindControl("clubLink") as HyperLink).NavigateUrl = "/Club/Detail.aspx?clubId=" + club.ClubId;
            (e.Row.FindControl("clubLink2") as HyperLink).NavigateUrl = "/Club/Detail.aspx?clubId=" + club.ClubId;
            (e.Row.FindControl("clubHead") as Label).Text = ClubRules.Instance.GetClubHeadByClubId(club.ClubId);
            (e.Row.FindControl("peopleAmount") as Label).Text = ClubRules.Instance.GetClubMemberAmountByClubId(club.ClubId).ToString() + "人";
            //(e.Row.FindControl("Des") as Label).Text = HtmlHelper.InterceptSubString(club.Club.Description,30,true);
        }
    }
}
